package com.ssyt.hiring.company.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.PageModel;
import com.ssyt.hiring.common.util.DateUtil;
import com.ssyt.hiring.company.entity.ApplicationEntity;
import com.ssyt.hiring.company.entity.PositionEntity;
import com.ssyt.hiring.company.service.ApplicationService;
import com.ssyt.hiring.company.service.PositionService;

public class ApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ApplicationService applicationService = new ApplicationService();
	private PositionService positionService = new PositionService();

	public ApplicationServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("apply".equals(command)) {
			apply(request, response);
		} else if ("listCompanyVerifyByPage".equals(command)) {
			listCompanyVerifyByPage(request, response);
		}else if("verify".equals(command)){
			verify(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	private void apply(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		int talentId = Constants.getTalentId(request, response);
		int row = applicationService.add(resumeId, positionId, talentId);
		if (row == 1) {
			request.setAttribute("success", "求职者的简历投递成功~");
		} else {
			request.setAttribute("error", "求职者的简历投递失败~");
		}

		response.sendRedirect("PositionServlet?command=detail&id=" + positionId);
	}

	/**
	 * 分页查询某个公司所有的职位申请信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listCompanyVerifyByPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int companyId = Constants.getCompanyId(request, response);
		ApplicationEntity application = new ApplicationEntity();
		application.setCompanyId(companyId);
		
		String positionName = request.getParameter("positionName");
		if (StringUtils.isNotBlank(positionName)) {
			application.setPosition$name(positionName);
		}
		String statusStr = request.getParameter("status");
		if (StringUtils.isNotBlank(statusStr)) {
			int status = Integer.parseInt(statusStr);
			application.setStatus(status);
		}
		
		String appTimeStart = request.getParameter("appTimeStart");
		if (StringUtils.isNotBlank(appTimeStart)) {
			application.setPosition$name(appTimeStart);
		}
		String appTimeEnd = request.getParameter("appTimeEnd");
		if (StringUtils.isNotBlank(appTimeEnd)) {
			application.setPosition$name(appTimeEnd);
		}
		
		int pageNo = Constants.DEFAULT_PAGE_NO;
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		int pageSize = Constants.DEFAULT_PAGE_SIZE;
		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr != null) {
			pageSize = Integer.parseInt(pageSizeStr);
		}

		PageModel<ApplicationEntity> pageModel = applicationService
				.listCompanyVerifyByPage(application, pageNo, pageSize);
		request.setAttribute("pageModel", pageModel);
		List<PositionEntity> positionList = positionService.listById(companyId);
		request.setAttribute("positionList", positionList);
		request.setAttribute("applicationCondition", application);
		request.getRequestDispatcher("WEB-INF/views/company/company-main.jsp")
				.forward(request, response);
	}
	
	/**
	 * 企业审核求职者的申请
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void verify(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int status = Integer.parseInt(request.getParameter("status"));
		int applicationId = Integer.parseInt(request
				.getParameter("applicationId"));
		int row = applicationService.verify(status, applicationId);

		if(row ==0){
			response.getWriter().print("fail");
		}else{
			response.getWriter().print("("+DateUtil.format(new Date())+")");
		}
	}
}
